#include "window.h"

/**
 * 设置窗口图标
 * 路径使用可执行程序相对路径
 */
void set_icon_png(cef_window_t *window, const char* file_path) {
    // 转换路径为Windows路径
    char *winPath = convertLinuxPathToWindows(file_path);
    // 获取全路径
    char *targetPath = get_target_path(winPath);

    char* png_data = NULL;
    size_t png_size = 0;

    if (read_png_file(targetPath, &png_data, &png_size) == 0) {
        LOG_ERROR("read_png_file error");
        return;
    }

    cef_image_t* image = cef_image_create();
    float scale_factor = 1.0f;
    int result = image->add_png(image, scale_factor, png_data, png_size);
    if (result > 0) {
        window->set_window_app_icon(window, image);
    } else {
        LOG_ERROR("pp->window->set_window_icon error");
    }

    // 堆资源释放
    free(winPath);
    // 释放下边数据会导致 引用异常
    // free(targetPath);
    // free(png_data);
    // image->base.release(&image->base);
}


/**
 * 设置窗口位置
 */
void set_position(cef_window_t *window, int x, int y) {
    cef_point_t origin = {x, y};
    window->base.base.set_position(&window->base.base, &origin);
}

/**
 * 设置窗口标题
 */
void set_title(cef_window_t *window, const char* title) {
    cef_string_utf16_t title_str = {};
    cef_string_utf8_to_utf16(title, strlen(title), &title_str);
    window->set_title(window, &title_str);
    // cef_string_utf16_clear(&title_str);
}
